{extend name="$base" /}
{block name="resources"}
<style>
.modal-infp-style{width:100%;margin:10px auto;}
.modal-infp-style table{width:100%;}
.modal-infp-style table tr td{border: 1px solid #e5e5e5;padding: 10px;}
.ns-main{margin-top: 0;}
</style>
{/block}
{block name="main"}
<table class="mytable">
	<tr>
		<th width="20%" style="text-align:left;"><button class="btn-common btn-small" onclick="location.href='{:__URL('ADMIN_MAIN/promotion/addgift')}';" style="margin:0 5px 0 0 !important;">添加赠品</button>
			<button class="btn-common-delete btn-small" onclick="batchDelete()" style="margin:0 5px 0 0 !important;">批量删除</button>
		</th>
		<th width="10%">
			<input type="text" id = 'search_text' placeholder="请输入活动名称" class="input-common" />
			<input type="button" onclick="searchData()" value="搜索" class="btn-common" />
		</th>
	</tr>
</table>
<table class="table-class">
	<colgroup>
	<col style="width: 2%;">
	<col style="width: 36%;">
	<col style="width: 8%;">
	<col style="width: 8%;">
	<col style="width: 8%;">
	<col style="width: 20%;">
	<col style="width: 16%;">
	</colgroup>
	<thead>
		<tr align="center">
			<th><i class="checkbox-common"><input type="checkbox"  ></i></th>
			<th align="left">活动名称</th>
			<th>活动状态</th>
			<th>已赠送</th>
			<th>已领取</th>
			<th>有效时间</th>
			<th>操作</th>
		</tr>
	</thead>
	<tbody></tbody>
</table>

<!-- 模态框（Modal） -->
<div class="modal fade hide" id="giftInfo" tabindex="-1" role="dialog" aria-labelledby="myModalLabel" aria-hidden="true">
	<div class="modal-dialog">
		<div class="modal-content">
			<div class="modal-header">
				<button type="button" class="close" data-dismiss="modal" aria-hidden="true">&times;</button>
				<h3>赠品详情</h3>
			</div>
			<div class="modal-body">
				<div class="modal-infp-style">
					<table>
						<tr>
							<td>活动名称</td>
							<td id="gift_name"></td>
						</tr>
						<tr>
							<td>有效期</td>
							<td id="time"></td>
						</tr>
						<tr>
							<td>领取有效期</td>
							<td id="days"></td>
						</tr>
						<tr>
							<td>领取限制</td>
							<td id="max_num"></td>
						</tr>
						<tr>
							<td colspan="2">赠品</td>
						</tr>
						<tr id="goods_list">
							<td></td>
							<td>商品名称</td>
						</tr>
					</table>
				</div>
			</div>
			<div class="modal-footer">
				<button type="button" class="btn btn-default" data-dismiss="modal">关闭</button>
			</div>
		</div>
	</div>
</div>
{/block}
{block name="script"}
<script type="text/javascript">
function LoadingInfo(page_index) {
	var search_text = $("#search_text").val();
	$.ajax({
		type : "post",
		url : "{:__URL('ADMIN_MAIN/promotion/giftlist')}",
		data : {
			"page_index" : page_index,
			"page_size" : $("#showNumber").val(),
			"search_text" : search_text
		},
		success : function(data) {
			if (data["data"].length > 0) {
				$(".table-class tbody").empty();
				for (var i = 0; i < data["data"].length; i++) {
					var html = '';
					html += '<tr align="center">';
					if(data['data'][i]['type'] != 1){
						html += '<td><i class="checkbox-common"><input name="sub" type="checkbox" value="'+ data["data"][i]["gift_id"] +'" ></i></td>';
					}else{
						html += '<td><i class="checkbox-common disabled"><input name="sub" type="checkbox" value="'+ data["data"][i]["gift_id"] +'" disabled></i></td>';
					}
					html += '<td align="left">' + data["data"][i]["gift_name"] + '</td>';;
					
					html += '<td>'+ data['data'][i]['type_name'] +'</td>';
					html += '<td> - </td>';
					html += '<td> - </td>';
					html += '<td>开始时间：' + timeStampTurnTime(data["data"][i]["start_time"]) + '<br/>结束时间：' + timeStampTurnTime(data["data"][i]["end_time"]) + '</td>';
					html += '<td><a href="'+__URL('ADMIN_MAIN/promotion/updategift?gift_id=' + data["data"][i]["gift_id"])+'">编辑</a>&nbsp;';
					html += '<a href="javascript:void(0);"onclick="giftInfo('+data["data"][i]["gift_id"]+')">详情</a> ';
					if(data['data'][i]['type'] != 1){
						html += '<a href="javascript:void(0);"onclick="deleteGift('+data["data"][i]["gift_id"]+')">删除</a>';
					}
					html += '<br/><a href="'+__URL('ADMIN_MAIN/promotion/giftGrantRecordsList?gift_id=' + data["data"][i]["gift_id"])+'")">发放记录</a> ';
					html += '</td>';
					html += '</tr>';
					$(".table-class tbody").append(html);
				}
			} else {
				var html = '<tr align="center"><td colspan="9">暂无符合条件的数据记录</td></tr>';
				$(".table-class tbody").html(html);
			}
			initPageData(data["page_count"],data['data'].length,data['total_count']);
			$("#pageNumber").html(pagenumShow(jumpNumber,$("#page_count").val(),{$pageshow}));
		}
	});
}

function searchData(){
	LoadingInfo(1);
}

function giftInfo(gift_id){
	$("#giftInfo").modal("show");
	$.ajax({
		type:"post",
		url : "{:__URL('ADMIN_MAIN/promotion/getgiftinfo')}",
		data : { "gift_id" : gift_id },
		success : function(data) {
			$("#gift_name").text(data["gift_name"]);
			$("#time").text(timeStampTurnTime(data['start_time'])+' 至 '+timeStampTurnTime(data['end_time']));
			$("#days").text(data["days"]+"天");
			$("#max_num").text(data["max_num"]+"次/人");
			var goods_list = '';
			goods_list += '<tr>';
			goods_list += '<td><img src="'+__IMG(data['gift_goods']['picture_info']['pic_cover_micro'])+'" /></td>';
			goods_list += '<td>'+data['gift_goods']['goods_name']+'</td>';
			goods_list += '</tr>';
			$("#giftInfo table tr:gt(5)").remove();
			$("#goods_list").after(goods_list);
		}
	})
}

function deleteGift(gift_id){
	$( "#dialog" ).dialog({
		buttons: {
			"确定": function() {
			$.ajax({
				type : "post",
				url : "{:__URL('ADMIN_MAIN/promotion/deleteGift')}",
				data : {"gift_id" : gift_id.toString()},
				success : function(data) {
					if(data["code"] > 0 ){
						LoadingInfo(1);
						showMessage('success', data["message"]);
					}else{
						showMessage('error', data["message"],window.location.reload());
					}
				}
			});
			$(this).dialog('close');
			},
			"取消,#f5f5f5,#666": function() {
				$(this).dialog('close');
			},
		},
		contentText:"确定要删除该赠品吗？",
	});
}

//批量删除
function batchDelete() {
	var ids = [];
	$(".table-class tbody input[type='checkbox']:checked").not(":disabled").each(function() {
		if (!isNaN($(this).val())) {
			ids.push($(this).val());
		}
	});

	if(ids.length ==0){
		$( "#dialog" ).dialog({
			buttons: {
				"确定,#0059d6,#fff": function() {
					$(this).dialog('close');
				}
			},
			contentText:"请选择需要操作的记录",
			title:"消息提醒",
		});
		return false;
	}
	deleteGift(ids);
}
</script>
{/block}